﻿package{
	import flash.display.*;
	import flash.ui.*
	import fl.controls.*;
	import flash.utils.*
	import flash.text.*;
	import MyUI.*;
	public class UserInterFace extends Sprite{
		var playerList,playerMenu,stateBar,myView,playerView;
		var coolBtn
		var superior;
		private var playerMenuFun:Array;
		private var playerUI:UIGroup = new UIGroup();
		function UserInterFace(parent){
			parent.addChild(this);
			superior = parent
			//周围玩家列表
			playerList = new MyMenu(this);
			playerList.x = 400;playerList.y = 100;
			playerList.setSize(200,300);
			playerList.callback = selectPlayer
			playerMenu = new MyContainer();
			playerMenu.layout = 'horizontal';
			playerMenu.paddingH = 5;
			playerMenu.x = 600;playerMenu.y = 480;
			addChild(playerMenu);
			playerMenuFun = [showPlayerList,showMyState]
			//状态栏
			stateBar = new MyFrame(this);
			stateBar.x = 400;stateBar.y = 100;
			stateBar.content.layout = [[0,150]];
			var obj = new MyContainer();
			obj.paddingV = 15;
			obj.append(new labelPair('名字'),new labelPair('等级'),new labelPair('公会'));
			stateBar.append(new Bitmap(),obj)
			stateBar.append(new labelPair('生命'),new labelPair('魔法'));
			stateBar.append(new labelPair('力量'),new labelPair('敏捷'),new labelPair('活力'),new labelPair('魔力'));
			stateBar.setSize(stateBar.content.wide+10,stateBar.content.high+30);
			//人物状态缩略
			myView = new overviewBar();
			addChild(myView);
			playerView = new overviewBar();
			playerView.x = 300;
			playerView.visible = false;
			addChild(playerView);
			//冷却按钮
			coolBtn = new coolingBtn();
			coolBtn.x = 400;coolBtn.y = 500;
			addChild(coolBtn);
			coolBtn.setSpeed(3);
			coolBtn.judge = function(){return superior.lock != null}
			coolBtn.begin = function(){superior.sendMsg('magic','fire',superior.my.name,superior.lock.name,superior.lock.info.type)};
		}
		function setIcon(pic,seq:int){
			var menuObj = new MyUIObject();
			menuObj.click(playerMenuFun[seq]);
			menuObj.addChild(pic);
			playerMenu.append(menuObj);
		}
		function showPlayerList(e = null){
			playerList.clear();
			for(var k in superior.playerList){
				playerList.addItem(k);
			}
			playerUI.show(playerList);
		}
		function showMyState(e = null){
			var myState = superior.info.state
			var hp = stateBar.content.getChildAt(2);
			hp.value.text = myState.HP + '/' + myState.MaxHP;
			var mp = stateBar.content.getChildAt(3);
			mp.value.text = myState.MP + '/' +  myState.MaxMP;
			playerUI.show(stateBar);
		}
		function setPortrait(pic){
			stateBar.content.getChildAt(0).bitmapData = pic.bitmapData;
			myView.getChildAt(0).bitmapData = pic.bitmapData;
		}
		function setPortraitName(){
			myView.getChildAt(1).text = superior.my.name;
		}
		function selectPlayer(value:String){
			superior.privacyBar.text = value;
			playerList.hidden();
		}
		function showPlayerInfo(role){
			var info = role.info
			if(info.avatar){
				var sldr = superior.newLoader(info.avatar);
				sldr.callback = function(){
					playerView.getChildAt(0).bitmapData = sldr.gut.bitmapData;
				}
			}else{
				playerView.getChildAt(0).bitmapData = null;
			}
			playerView.getChildAt(1).text = role.name;
			var HP = playerView.getChildAt(2);
			HP.max = info.MaxHP;HP.nonce = info.HP;
			var MP = playerView.getChildAt(3);
			MP.max = info.MaxMP;MP.nonce = info.MP;
			playerView.visible = true;
		}
		function showMyInfo(){
			var info = superior.my.info;
			var HP = myView.getChildAt(2);
			HP.max = info.MaxHP;HP.nonce = info.HP;
			var MP = myView.getChildAt(3);
			MP.max = info.MaxMP;MP.nonce = info.MP;
		}
		function closePlayerInfo(){
			superior.lock = null;
			playerView.visible = false;
		}
	}
}

	import flash.display.*;
	import flash.ui.*
	import fl.controls.*;
	import flash.utils.*
	import flash.text.*;
	import flash.events.*;
	
	class dataBar extends Shape{
		var max:Number;
		var now:Number = 0;
		var length:Number;
		var border:uint = 0x1F2B00;
		var inStart:uint,inEnd:uint;
		function dataBar(length:Number = 100){
			this.length = length;
		}
		function set nonce(v:Number){
			try{
			now = v;
			graphics.clear();
			graphics.lineStyle(1,border);
			//graphics.drawRect(0,0,length+2,15);
			graphics.drawRoundRect(0,0,length+2,15,10);
			//graphics.beginFill(inner);
			graphics.beginGradientFill(GradientType.LINEAR,[inStart,inEnd],[1,1],[150,255],null);
			//graphics.drawRect(1,1,length*now/max,13);
			graphics.drawRoundRect(1,1,length*now/max,13,10);
			graphics.endFill();
			}catch(e){
				trace('vale:',length,now,max);
			}
			
		}
	}
	class labelPair extends Sprite{
		var value:TextField
		function labelPair(label){
			var tag:TextField = new TextField();
			tag.text = label;
			tag.width = 50;
			tag.height = 20;
			tag.selectable = false;
			addChild(tag)
			value = new TextField();
			value.x = tag.width;
			value.width = 100;
			value.height = 20;
			value.selectable = false;
			addChild(value)
		}
	}
	class overviewBar extends Sprite{
		function overviewBar(){
			addChild(new Bitmap());
			var txt = new TextField();
			txt.width = 100;txt.height = 20;
			txt.x = 70;txt.y = 10;
			addChild(txt);
			var obj = new dataBar();
			obj.x = 70;obj.y = 30;
			obj.inStart =0x24C328;obj.inEnd = 0x00FF01;
			obj.max = 100;obj.nonce = 100;
			addChild(obj);
			obj = new dataBar();
			obj.x = 70;obj.y = 50;
			obj.inStart =0x4A6AFF;obj.inEnd = 0x00CAFF;
			obj.max = 100;obj.nonce = 100;
			addChild(obj);
		}
	}
	class coolingBtn extends Sprite{
		var maskShape:Shape = new Shape();
		var speed:Number;
		var arc:Number;
		var pic:Bitmap;
		var graphic:Graphics;
		var begin:Function,judge:Function;
		const endArc:Number = Math.PI*1.5;
		private var lastX:Number,lastY:Number,flag:Boolean = false;
		private var r:Number;
		function coolingBtn(){
			pic = new Bitmap();
			addChild(pic);
			var g:Shape = new Shape();
			graphic = g.graphics;
			addChild(g);
			mask = maskShape;
			addChild(maskShape);
			addEventListener(MouseEvent.CLICK,start);
		}
		public function setIcon(bitmap:Bitmap){
			pic.bitmapData = bitmap.bitmapData;
			pic.x = -pic.width/2;pic.y = -pic.height/2;
			maskShape.graphics.beginFill(0xFFFFFF);
			maskShape.graphics.drawRect(pic.x,pic.y,pic.width,pic.height);
			maskShape.graphics.endFill();
			r = Math.sqrt(pic.width*pic.width+pic.height*pic.height)/2
		}
		function setSpeed(s:Number){
			var fps = int(s*stage.frameRate);
			speed = Math.PI*2/fps;
		}
		function start(e = null){
			if(flag)return;
			if(!judge())return
			flag = true;
			begin();
			arc = -Math.PI/2;
			lastX = 0;lastY = r;
			addEventListener(Event.ENTER_FRAME,run);
		}
		function run(e:Event){
			if(arc >= endArc){
				removeEventListener(Event.ENTER_FRAME,run);
				graphic.clear();
				flag = false;
			}
			arc+=speed;
			var nx:Number = Math.cos(arc)*r;
			var ny:Number = Math.sin(arc)*r;
			graphic.beginFill(0,0.3);
			graphic.lineTo(lastX,lastY);
			graphic.lineTo(nx,ny);
			graphic.lineTo(0,0);
			graphic.endFill();
			lastX = nx;lastY = ny;
		}
	}
